from __future__ import absolute_import
import os
import logging
from forest.registry import register_tool
from forest.tools.system.remove import remove
logger = logging.getLogger('tools.system.ln')

@register_tool('system.ln')
def link(src, dst, force = True, no_exists=False):
    ''' creates a symbolic link. no other options from ln are supported
        (since it uses python's os API, rather than the system command)
    '''
    logger.debug('Link node: %s to %s', src, dst)

    # DONE: it would be nice if we had ln -s --force here
    if force:
        remove(dst)

    if not no_exists and not os.path.lexists(src):
        logger.warn('No such file %s, skipping.', src)
        return dst

    os.symlink(src, dst)

    return dst
